# Create your views here.
from django.core.paginator import Paginator
from django.shortcuts import render, HttpResponseRedirect

from login.models import *


def index(request, pIndex):
    try:
        news = News.objects.filter(is_active=True).order_by('-time')
        type = Type.objects.filter(is_active=True)
        href = Href.objects.filter(is_active=True)
        MyHref = MyHref1.objects.filter(is_active=True)
        admiration = Admiration.objects.filter(is_active=True)
    except Exception as e:
        print(e)
    kw = request.GET.get("kw")
    # print(kw)
    if kw:
        news = news.filter(type_id=kw)
        # 执行分页处理

    pIndex = int(pIndex)
    page = Paginator(news, 9)  # 以每页9条数据分页
    maxpagex = page.num_pages  # 获取最大页数
    # 判断当前页是否越界
    if pIndex > maxpagex:
        pIndex = maxpagex
    if pIndex < 1:
        pIndex = 1

    list2 = page.page(pIndex)  # 获取当前页数据
    plist = page.page_range  # 获取页码表信息
    context = {"plist": plist, "pIndex": pIndex, "max_pages": maxpagex,
               "news": news, "type": type, "href": href, "myhref": MyHref, "admiration": admiration}
    return render(request, 'login/index.html', context)


def my(request):
    return HttpResponseRedirect('news/1')


def login(request):
    if request.method == 'POST':
        name = request.POST['name']
        password = request.POST['password']
        try:
            user = User.objects.filter(name=name, password=password)
        except Exception as e:
            print(e)
        if user:
            name = request.session['name'] = name
            media = str(user.get(name=name).photo)
            name = request.session['media'] = media
            return HttpResponseRedirect('/')
        else:
            msg = "账号或密码错误！！"
            return render(request, 'login/login.html', {"msg": msg})

    else:
        return render(request, 'login/login.html')


def register(request):
    if request.method == 'POST':
        name = request.POST['name']
        password = request.POST['password']
        phone = request.POST['phone']
        photo = request.FILES['photo']
        # return HttpResponse("成功！")
        try:
            user = User.objects.filter(name=name)
        except Exception as e:
            print(e)
        if user:
            msg = '用户已存在！！'
            return render(request, 'login/register.html', {"msg": msg})
        else:
            try:
                user = User.objects.create(
                    name=name,
                    password=password,
                    phone=phone,
                    photo=photo

                )
            except Exception as e:
                print(e)
        msg = "注册成功！"

        return render(request, 'login/login.html', {"msg": msg})
    else:
        return render(request, 'login/register.html')


def logout(request):
    if 'name' in request.session:
        del request.session['name']
    if 'media' in request.session:
        del request.session['media']
    return HttpResponseRedirect('/login')


def psed_update(request):
    if request.method == "POST":
        name = request.session.get('name')
        password = request.POST['password']
        password1 = request.POST['password1']
        try:
            user = User.objects.get(name=name)
        except Exception as e:
            print(e)
        if user.password == password:
            user.password = password1
            user.save()
            return HttpResponseRedirect('/logout')
        else:
            msg = "原密码错误，请重新输入！"
            return render(request, 'login/pswd_update.html', {"msg": msg})
    else:
        return render(request, 'login/pswd_update.html')
